Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
The pkg-dir npm package is designed to help developers find the root directory of a Node.js project or npm package. This is particularly useful in situations where your code needs to access project-wide resources or configurations, and you need a reliable way to locate the root directory of the project, regardless of the current working directory of the process.
Find the root directory of a project
This feature allows you to asynchronously find the root directory of a project by passing the current directory to the function. It returns a promise that resolves with the path to the nearest package directory.
const pkgDir = require('pkg-dir');
(async () => {
const rootDir = await pkgDir(__dirname);
console.log(rootDir);
})();
Synchronous finding of the project root directory
For scenarios where asynchronous code is not ideal, pkg-dir provides a synchronous method to find the project root directory. This method is useful in synchronous scripts or module initialization.
const pkgDir = require('pkg-dir');
const rootDir = pkgDir.sync(__dirname);
console.log(rootDir);
find-root is a package with a similar purpose to pkg-dir, aiming to find the root directory of a Node.js project by looking for a 'package.json' file. Compared to pkg-dir, find-root only offers a synchronous API and does not provide an asynchronous method.
app-root-path provides functionality to determine the root path of the current Node.js application. Unlike pkg-dir, which is focused on finding the nearest package directory, app-root-path is designed to find the application's root directory. It offers both synchronous and asynchronous methods, but its approach and use cases can be slightly different, focusing more on application root rather than package root.
Find the root directory of a Node.js project or npm package
$ npm install pkg-dir
/
└── Users
└── sindresorhus
└── foo
├── package.json
└── bar
├── baz
└── example.js
// example.js
const pkgDir = require('pkg-dir');
(async () => {
const rootDir = await pkgDir(__dirname);
console.log(rootDir);
//=> '/Users/sindresorhus/foo'
})();
Returns a Promise
for either the project root path or undefined
if it couldn't be found.
Returns the project root path or undefined
if it couldn't be found.
Type: string
Default: process.cwd()
Directory to start from.
FAQs
Find the root directory of a Node.js project or npm package
The npm package pkg-dir receives a total of 48,006,734 weekly downloads. As such, pkg-dir popularity was classified as popular.
We found that pkg-dir demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.